package com.rock.code.common.util;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 * Created by zhongqing.han Cotter on 2019/4/25.
 */
public class ResDataCase {

    public void getPool(String path, String fileName, String unitId) {

        String _unitId = unitId;

        File xml = new File(fileName);
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        Element root = document.getRootElement();
        Element GRID = root.element("GRID");
        List<Element> TRs = GRID.element("GBODY").elements();

        for(Element TR : TRs) {

            WriteKPI writeKPI = new WriteKPI();

            String name = "";
            List<Element> TDs = TR.elements();
            for(Element TD : TDs){
                String _name = TD.element("name") != null ? TD.element("name").getText() : "";
                if(!"".equals(_name)){
                    name = _name;
                }
            }
            for(Element TD : TDs){

                String dataTime = writeKPI.getNowTime();

                List<Element> state = TD.elements("state");
                writeDate(state, "PM-10-19-012-01", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> names = TD.elements("name");
                writeDate(names, "CM-10-19-012-02", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> status = TD.elements("status");
                writeDate(status, "PM-10-19-012-03", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> ip_port = TD.elements("ip_port");
                writeDate(ip_port, "CM-10-19-012-04", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> client_se_new_count = TD.elements("client_se_new_count");
                writeDate(client_se_new_count, "PM-10-19-012-05", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> client_se_count = TD.elements("client_se_count");
                writeDate(client_se_count, "PM-10-19-012-06", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> service_se_new_count = TD.elements("service_se_new_count");
                writeDate(service_se_new_count, "PM-10-19-012-07", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> service_se_count = TD.elements("service_se_count");
                writeDate(service_se_count, "PM-10-19-012-08", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> rx_flux = TD.elements("rx_flux");
                writeDate(rx_flux, "PM-10-19-012-09", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> tx_flux = TD.elements("tx_flux");
                writeDate(tx_flux, "PM-10-19-012-10", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> http_tps = TD.elements("http_tps");
                writeDate(http_tps, "PM-10-19-012-11", _unitId+"-"+name, writeKPI ,dataTime);
                List<Element> usr_count= TD.elements("usr_count");
                writeDate(usr_count, "PM-10-19-012-12", _unitId+"-"+name, writeKPI ,dataTime);
            }

            WriteFile wf = new WriteFile();
            wf.writeSevenStrucFile(path, writeKPI.getVECTOR());

        }

    }

    public void writeDate(List<Element> data, String _kpi_id, String unitId, WriteKPI writeKPI, String dataTime){
        for(Element _data : data){
            if(!StringUtils.isBlank(_data.getText())){
                writeKPI.writeKPItoList(_data.getText(), _kpi_id, unitId, dataTime, "1");
            }
        }

    }

    public void writeFileUtf8(String sFileName, String sFileContent) {
        try {
            File file_pm = new File(sFileName);
            FileOutputStream fos = new FileOutputStream(file_pm);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
            BufferedWriter fw = new BufferedWriter(osw);
            fw.write(sFileContent);
            fw.close();
            osw.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
